//
//  SAShopAllocateCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/4/19.
//

#import "SAShopAllocateCell.h"
#import "SASalesOrderViewListModel.h"

@interface SAShopAllocateCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *minusBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numTf;

@end

@implementation SAShopAllocateCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASalesOrderToPendSkuList *)model {
    _model = model;
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.colourName,model.sizeName];
    self.numTf.text = model.stock;

}

#pragma mark - event
-(void)clickedMinusBtn
{
    NSInteger num = [self.model.stock integerValue] - 1;
    if (num < 0) {
        num = 0;
    }
    self.model.stock = [NSString stringWithFormat:@"%zd",num];
    self.numTf.text = self.model.stock;
    !self.countBlock ?: self.countBlock();
}

-(void)clickedAddBtn {
    NSInteger num = [self.model.stock integerValue] + 1;
    self.model.stock = [NSString stringWithFormat:@"%zd",num];
    self.numTf.text = self.model.stock;
    !self.countBlock ?: self.countBlock();
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField.text.length == 0) {
        self.model.stock = @"";
    }else {
        NSInteger num = [textField.text integerValue];
        self.model.stock = [NSString stringWithFormat:@"%zd",num];
        self.numTf.text = self.model.stock;
    }
    !self.countBlock ?: self.countBlock();
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 1) {
        return NO;
    }
    if ([textField.text isEqualToString:@"0"]) {
        textField.text = @"";
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self endEditing:YES];
   return YES;
}

#pragma mark - initUI
-(void)initUI {
    self.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.line2];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.minusBtn];
    [self.contentView addSubview:self.addBtn];
    [self.contentView addSubview:self.numTf];
}

-(UIView *)line2 {
    if (!_line2) {
        _line2 = [[UIView alloc] initWithFrame:CGRectMake(kScaleNum(275), kScaleNum(35), kScaleNum(40), kScaleNum(1))];
        _line2.backgroundColor = kColor(220, 220, 220, 1);
    }
    return _line2;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 200, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(15);
    }
    return _titleLab;
}

-(UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_minusBtn setImage:[UIImage imageNamed:@"shop_cutBtn"] forState:UIControlStateNormal];
        _minusBtn.frame = SARectMake(240, 0, 40, 40);
        _minusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_minusBtn addTarget:self action:@selector(clickedMinusBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _minusBtn;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"shop_addBtn"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(310, 0, 40, 40);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(UITextField *)numTf {
    if (!_numTf) {
        _numTf = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(270), kScaleNum(0), kScaleNum(50), kScaleNum(40))];
        _numTf.textColor = kBlackColor;
        _numTf.textAlignment = NSTextAlignmentCenter;
        _numTf.font = kFont(15);
        _numTf.delegate = self;
        _numTf.keyboardType = UIKeyboardTypeNumberPad;
        _numTf.returnKeyType = UIReturnKeyDone;
        [_numTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _numTf;
}

@end
